#ifndef F_CACeciliaSensorDemoC_h
#define F_CACeciliaSensorDemoC_h

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



class EDROOMTopContext {

protected:

enum EDROOMCACeciliaSensorDemoSignal { EDROOMSignalTimeout, EDROOMSignalDestroy, SDataFrame, SSignalFrame, SMsgCommited };


	friend class CACeciliaSensorDemo;

	CACeciliaSensorDemo &EDROOMcomponent;

	CEDROOMMessage * &Msg;

	CEDROOMMessage * &MsgBack;

	//Ports

	CEDROOMInterface & Ssi;
	CEDROOMTimingInterface & Timer;
	CEDROOMTimingInterface & ReTx;

	enum edroomStateId{I, StandBy, Listening};

	enum IdTrans{Init, SendFrame, SendFrame_SendData, SendFrame_SendAck, ReceiveSignal, ReceiveSignal_CrcFail, ReceiveSignal_IsAck, ReceiveData, ReceiveData_CrcFail, ReceiveData_IsData, RTxData,  EDROOMMEMORIA };

	//Constantes



	//Variables

	TEDROOMBool &VCrcOk;
	CDFrame &VDataFrame;
	bool_t &VEndOk;
	bool_t &VEndSend;
	ifstream &VFile;
	ifstream &VFile2;
	uint8_t &VFirstByte;
	uint8_t &VFirstFile;
	FrameTypeReceived_t &VFrameSend;
	CEDROOMTimer &VIdTimer;
	int &VMessageNumber;
	uint8_t &VnextFrameBytes;
	uint8_t &VnextFrameBytes2;
	int &VNumFile;
	uint64_t &VPendingBytes;
	uint8_t &VPendingBytesSensor2;
	TEDROOMByte *VSensor1Data;
	TEDROOMByte *VSensor2Data;
	int &VSensorId;
	TEDROOMUInt16 &VSensorIDRxTC;
	CDSignal &VSignalFrame;


	//Pool

	class CEDROOMPOOLCDFrame:public CEDROOMProtectedMemoryPool {
		public:
		CEDROOMPOOLCDFrame(TEDROOMUInt32 elemCount,CDFrame *pMem, TEDROOMBool *pMarks);
		CDFrame	* AllocData();
	};
	CEDROOMPOOLCDFrame	& EDROOMPoolCDFrame;
	class CEDROOMPOOLCDSignal:public CEDROOMProtectedMemoryPool {
		public:
		CEDROOMPOOLCDSignal(TEDROOMUInt32 elemCount,CDSignal *pMem, TEDROOMBool *pMarks);
		CDSignal	* AllocData();
	};
	CEDROOMPOOLCDSignal	& EDROOMPoolCDSignal;


	// CONSTRUCTORES

	EDROOMTopContext (CACeciliaSensorDemo &act, TEDROOMBool & EDROOMpVarVCrcOk, CDFrame & EDROOMpVarVDataFrame, bool_t & EDROOMpVarVEndOk, bool_t & EDROOMpVarVEndSend, ifstream & EDROOMpVarVFile, ifstream & EDROOMpVarVFile2, uint8_t & EDROOMpVarVFirstByte, uint8_t & EDROOMpVarVFirstFile, FrameTypeReceived_t & EDROOMpVarVFrameSend, CEDROOMTimer & EDROOMpVarVIdTimer, int & EDROOMpVarVMessageNumber, uint8_t & EDROOMpVarVnextFrameBytes, uint8_t & EDROOMpVarVnextFrameBytes2, int & EDROOMpVarVNumFile, uint64_t & EDROOMpVarVPendingBytes, uint8_t & EDROOMpVarVPendingBytesSensor2, TEDROOMByte * EDROOMpVarVSensor1Data, TEDROOMByte * EDROOMpVarVSensor2Data, int & EDROOMpVarVSensorId, TEDROOMUInt16 & EDROOMpVarVSensorIDRxTC, CDSignal & EDROOMpVarVSignalFrame, CEDROOMPOOLCDFrame & EDROOMpPoolCDFrame, CEDROOMPOOLCDSignal & EDROOMpPoolCDSignal );

	EDROOMTopContext ( EDROOMTopContext &contex );

public:

	bool EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans);

	void EDROOMNuevoMens () { EDROOMcomponent.EDROOMNewMsg(); }

	// Funciones Definidas por el usuario

	void	FBuildAck(byte_t *pFinalFrame);

	void	FBuildDiscovery(byte_t *pFinalFrame);

	void	FCheckFiles();

	uint64_t	FfileSize( const char* filePath );

	bool_t	FGetFrames(byte_t *pMessage);

	void	FIni();

	void	FIntToChar(int32_t num,byte_t * pConvert);

	void	FReceiveData();

	void	FReceiveSignal();

	void	FRtxRandom();

	void	FSendFrame();

	void	FSTimeSend();

	void	FStoreFrame();

	void	FStrCat(char * aux1, char * aux2);

	void	FStrCopy(char * aux1, char * aux2);

	TEDROOMBool	GlsAck();

	TEDROOMBool	GlsNotAck();

};

// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



class EDROOMTopState : public EDROOMTopContext {

protected:



	EDROOMTopContext::edroomStateId edroomCurrentState;

	EDROOMTopContext::edroomStateId edroomNextState;

	//Variables

	TEDROOMBool VCrcOk;
	CDFrame VDataFrame;
	bool_t VEndOk;
	bool_t VEndSend;
	ifstream VFile;
	ifstream VFile2;
	uint8_t VFirstByte;
	uint8_t VFirstFile;
	FrameTypeReceived_t VFrameSend;
	CEDROOMTimer VIdTimer;
	int VMessageNumber;
	uint8_t VnextFrameBytes;
	uint8_t VnextFrameBytes2;
	int VNumFile;
	uint64_t VPendingBytes;
	uint8_t VPendingBytesSensor2;
	TEDROOMByte VSensor1Data[2048];
	TEDROOMByte VSensor2Data[2048];
	int VSensorId;
	TEDROOMUInt16 VSensorIDRxTC;
	CDSignal VSignalFrame;


	//Pools

	CEDROOMPOOLCDFrame	EDROOMPoolCDFrame;
	CEDROOMPOOLCDSignal	EDROOMPoolCDSignal;


public:

	// CONSTRUCTOR

	EDROOMTopState (CACeciliaSensorDemo &act, CEDROOMMemory *pEDROOMMemory  );


	// EDROOMBehaviour**********************************************

	void EDROOMBehaviour();

	// Inicio**********************************************

	void EDROOMInicio();

	// ***********************************************************************

	// SubEstado Inicial 

	// ***********************************************************************



	TEDROOMTransId EDROOMIllegada();

	// ***********************************************************************

	// SubEstado Hoja StandBy

	// ***********************************************************************



	TEDROOMTransId EDROOMStandByllegada();

	// ***********************************************************************

	// SubEstado Hoja Listening

	// ***********************************************************************



	TEDROOMTransId EDROOMListeningllegada();

};


#endif
